package com.example.paging3samples.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.map
import com.example.paging3samples.database.PostDao
import com.example.paging3samples.database.PostDatabase
import com.example.paging3samples.database.mapper.toPost
import com.example.paging3samples.database.repository.PostRepository
import com.example.paging3samples.network.ApiService
import com.example.paging3samples.network.PageResult
import com.example.paging3samples.network.CustomResult
import com.example.paging3samples.network.PagingSampleRepo
import com.example.paging3samples.network.PostRemoteMediator
import com.example.paging3samples.network.PostsPagingSource
import com.example.paging3samples.network.model.konachan.KoNaChan
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@HiltViewModel
class MainViewModel
@Inject constructor(
    private val pagingSampleRepo: PagingSampleRepo,
    private val apiService: ApiService,
    private val database: PostDatabase
) : ViewModel() {

    private val githubRepoLiveData = MutableLiveData<CustomResult<PageResult>>()

    fun observeData(): MutableLiveData<CustomResult<PageResult>> {
        return githubRepoLiveData
    }

    var koNaChan = MutableLiveData<List<KoNaChan>>()
        private set

    fun getReposFromGitHub() {
        viewModelScope.launch {
//            pagingSampleRepo.getCategories(liveData = githubRepoLiveData, query = "")
        }
    }

    init {

    }

    private val pagingSourceFactory = {
        // 数据源，要求返回的是 PagingSource 类型对象
        database.postDao.getPosts()
    }

    private fun getKoNaChanPosts() {
        viewModelScope.launch {
            pagingSampleRepo.getKoNaChanPost().map {
                koNaChan.postValue(it)
            }
        }

    }

    @OptIn(ExperimentalPagingApi::class)
    fun getPosts():
            Flow<PagingData<UIModel>> {
        return Pager(
            config = PagingConfig(
                pageSize = 5,
                enablePlaceholders = false
            ),
            pagingSourceFactory = pagingSourceFactory,
            remoteMediator = PostRemoteMediator(apiService, database)
        ).flow
            /** Mapping Data into local Models */
            .map { pagingData ->
                pagingData.map {
                    UIModel.CategoryModel(it.toPost()) as UIModel
                }
            }.cachedIn(viewModelScope)

    }

//    fun getPosts():
//            Flow<PagingData<UIModel>> {
//        return Pager(
//            config = PagingConfig(
//                pageSize = 5,
//                enablePlaceholders = false
//            ),
//            pagingSourceFactory = { PostsPagingSource(apiService) }
//        ).flow
//            /** Mapping Data into local Models */
//            .map { pagingData ->
//                pagingData.map {
//                    UIModel.CategoryModel(it) as UIModel
//                }
//            }.cachedIn(viewModelScope)
//
//    }


}